package com.foreveross.proxyip.util;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 使用正则表达式
 * @author lyq
 *
 */
public class RegUtil {
	
	public static final Log LOG = LogFactory.getLog(RegUtil.class);
	
	/**
	 * 提起str第一个ip:port 字符串
	 * @param str
	 * @return
	 */
	public static String getIpPort(String str){
		 Pattern p = Pattern.compile("[\\d]{1,3}[\\.][\\d]{1,3}[\\.][\\d]{1,3}[\\.][\\d]{1,3}[:][\\d]{1,6}",Pattern.CASE_INSENSITIVE+Pattern.DOTALL);
		    Matcher m = p.matcher(str);
		     if(m.find()){
		    	 return m.group(0);
		    	}
		     return null;
	}
	/**
	 * 字符串是否ip
	 * @param str
	 * @return
	 */
	public static boolean isIp(String str){
		Pattern p = Pattern.compile("[\\d]{1,3}[\\.][\\d]{1,3}[\\.][\\d]{1,3}[\\.][\\d]{1,3}",Pattern.CASE_INSENSITIVE+Pattern.DOTALL);
		 Matcher m = p.matcher(str);
		 return m.find();
	}
	/**
	 * 判断字符串是否正整数,if(null) return false;
	 * @param str
	 * @return
	 */
	public static boolean isNumber(String str){
		if(str==null){
			return false;
		}
		 Pattern p = Pattern.compile("\\D",Pattern.CASE_INSENSITIVE+Pattern.DOTALL);
		 Matcher m = p.matcher(str);
		 return !m.find();//不能找到非数字，返回true
	}
}
